package com.wbl.train.batch.job;

import cn.hutool.core.collection.CollUtil;
import com.wbl.train.common.feign.BusinessFeign;
import com.wbl.train.common.business.vo.TrainVo;
import com.wbl.train.common.exception.ApplicationException;
import com.wbl.train.common.result.ResultCode;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Author: 王博纶
 * Date: 2024/2/14 15:23
 * FileName: DailyTrainJob
 * Description: 生成火车座位基础数据
 */
@Slf4j
@DisallowConcurrentExecution
public class GenTrainSeatJob implements Job {

    @Resource
    BusinessFeign businessFeign;

    @Override
    @Transactional
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info("生成火车座位基础数据 开始");
        String trainCode = null;
        // 获取火车车次列表
        List<TrainVo> allTrain = businessFeign.getAll().getData();
        if(CollUtil.isEmpty(allTrain)){
            log.error("没有火车车次的信息");
            throw new ApplicationException(ResultCode.DATA_NOT_EXIST,"没有火车车次的信息");
        }
        // 在循环中获取火车车次的编号
        for (TrainVo trainVo : allTrain) {
            trainCode = trainVo.getCode();
            //调用生成车次的方法
            businessFeign.generateCarriageAndSeat(trainCode);
        }
        log.info("生成火车座位基础数据 结束");
    }
}
